<template>
  <div class="common-header">
    <span class="pageName">
      <!-- {{ $route.name }} -->
    </span>
    <div class="options">
      <div class="user">
        <img class="avatar" v-if="avatar" :src="avatar" alt />
        <img v-else class="avatar" src="@/assets/default-avatar.png" alt />
        <span class="userName" :title="$store.state.userInfo.username">
          {{ $store.state.userInfo.username }}
        </span>
      </div>
      <div class="editPass" @click="$router.push('/editPass')">
        <span> 修改密码 </span>
      </div>
      <span
        class="loginOut"
        @click="loginOut"
        style="margin-left: 15px; padding-top: 3px"
      >
        <img
          src="@/assets/logOut.svg"
          style="width: 18px; height: 20px; margin: 0 5px"
          alt=""
        />
        退出
      </span>
    </div>
  </div>
</template>

<script>
import util from "@/utils/auth";
import api from "@/api/user.js";
import { client } from "@/utils/oss";
export default {
  data() {
    return {
      avatar: "",
    };
  },
  watch: {
    "$route.path"() {},
  },
  mounted() {
    if (this.$store.state.userInfo.avatar) {
      this.avatar = client(this.$store.state.ossToken).signatureUrl(
        this.$store.state.userInfo.avatar
      );
    }
  },
  methods: {
    loginOut() {
      api.logout().then((res) => {
        util.removeToken();
        sessionStorage.clear();
        this.$router.push("/login");
      });
    },
  },
};
</script>

<style lang="less">
.el-badge__content.el-badge__content--undefined.is-fixed {
  transform: translateX(100%) translateY(-50%) scale(0.7);
}
.common-header {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #fff;
  box-shadow: 0px 1px 4px 0px rgba(0, 21, 41, 0.12);
  .pageName {
    font-size: 18px;
    padding-left: 32px;
  }
  .options {
    display: flex;
    padding-right: 32px;
    font-size: 14px;
    .user {
      display: flex;
      align-items: center;
      cursor: pointer;
      .avatar {
        width: 24px;
        height: 24px;
        border-radius: 50%;
        margin-right: 8px;
      }
      .userName {
        max-width: 100px;
        overflow: hidden; //超出的文本隐藏
        text-overflow: ellipsis; //溢出用省略号显示
        white-space: nowrap; //溢出不换行
      }
    }
    .el-badge {
      margin: 0 30px;
      cursor: pointer;
    }
    .editPass {
      cursor: pointer;
      margin: 0 20px;
      line-height: 24px;
    }
    .loginOut {
      cursor: pointer;
      display: flex;
      justify-content: center;
    }
  }
}
</style>
